package com.aglow.bluetoothspeaker.main.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aglow.bluetoothspeaker.R;
import com.aglow.bluetoothspeaker.model.bean.Music;
import com.aglow.bluetoothspeaker.presenter.lisnter.OnItemClickListener;
import com.alex.alexswitch.ISwitch;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MusicListAdapter extends RecyclerView.Adapter<MusicListAdapter.ViewHolder> {
    private final Context mContext;
    private final List<Music> list;
    private final LayoutInflater inflater;
    @BindView(R.id.id_apple_is)
    ISwitch idAppleIs;
    @BindView(R.id.view2)
    View view2;
    @BindView(R.id.textView5)
    TextView textView5;
    @BindView(R.id.imageView4)
    ImageView imageView4;
    @BindView(R.id.imageView3)
    ImageView imageView3;
    private OnItemClickListener mItemClickListener;

    public MusicListAdapter(Context context, List<Music> list) {
        this.mContext = context;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_music, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mItemClickListener.onItemClick(view);
            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Music music = list.get(position);
        holder.tvName.setText(TextUtils.isEmpty(music.getAlbum()) ? "unknown" : music.getArtist());
        //处理歌手名重复的问题
        holder.tvInfo.setText(music.getFileName().substring(music.getFileName().indexOf("-")+1));
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_info)
        TextView tvInfo;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
